<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="所属国家" prop="country">
        <el-select placeholder="请输入所属国家" filterable v-model="queryParams.country">
          <el-option-group v-for="group in country":key="group.label" :label="group.label">
            <el-option v-for="item in group.options" :key="item.value" :label="item.label" :value="item.label">
              <span style="float: left">{{ item.label }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{ item.value }}</span>
            </el-option>
          </el-option-group>
        </el-select>
      </el-form-item>
      <el-form-item label="数据名称" prop="webTitle">
        <el-input
          v-model="queryParams.webTitle"
          placeholder="请输入数据名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="数据格式" prop="sourceType">
        <el-select v-model="queryParams.sourceType" placeholder="请选择数据格式" clearable size="small">
          <el-option
            v-for="dict in dict.type.source_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="数据类型" prop="dataType">
        <el-select v-model="queryParams.dataType" placeholder="请选择数据类型" clearable size="small">
          <el-option
            v-for="dict in dict.type.data_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="研究领域" prop="resarchArea">
        <el-select v-model="queryParams.resarchArea" placeholder="请选择研究领域" clearable size="small">
          <el-option
            v-for="dict in dict.type.academic_area"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
          <el-option
            v-for="dict in dict.type.sys_job_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['information:sources:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['information:sources:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['information:sources:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['information:sources:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="sourcesList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="sourceId" />
      <el-table-column label="格式" align="center" prop="sourceType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.source_type" :value="scope.row.sourceType"/>
        </template>
      </el-table-column>
      <el-table-column label="类型" align="center" prop="dataType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.data_type" :value="scope.row.dataType"/>
        </template>
      </el-table-column>
      <el-table-column label="所属国家" align="center" prop="country" />
      <el-table-column label="数据名称" align="center" prop="webTitle" />
      <el-table-column label="栏目名称" align="center" prop="columnTitle" />
      <el-table-column label="研究领域" align="center" prop="resarchArea">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.academic_area" :value="scope.row.resarchArea"/>
        </template>
      </el-table-column>
      <el-table-column label="检索标签" >
        <template slot-scope="scope">
          <div v-for="(val,index) in scope.row.labels" :key="index">
            <span>{{val.labelName}}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_job_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="采集时间" align="center" prop="lastUpdate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.lastUpdate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-setting"
            @click="handleLabel(scope.row)"
            v-hasPermi="['information:sources:edit']"
          >设置</el-button> -->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['information:sources:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['information:sources:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改数据源管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="200px">
        <el-form-item label="数据类型" prop="dataType">
          <el-select v-model="form.dataType" placeholder="请选择数据类型">
            <el-option
              v-for="dict in dict.type.data_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="数据格式" prop="sourceType">
          <el-select v-model="form.sourceType" placeholder="请选择数据格式">
            <el-option
              v-for="dict in dict.type.source_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="信息分类" prop="infoType">
          <el-select v-model="form.infoType" placeholder="请选择信息分类">
            <el-option
              v-for="dict in dict.type.disaster_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="所属国家" prop="country">
          <el-select  placeholder="请输入所属国家" filterable style="width: 77.9%" v-model="form.country">
            <el-option-group v-for="group in country":key="group.label" :label="group.label">
              <el-option v-for="item in group.options" :key="item.value" :label="item.label" :value="item.label">
                <span style="float: left">{{ item.label }}</span>
                <span style="float: right; color: #8492a6; font-size: 13px">{{ item.value }}</span>
              </el-option>
            </el-option-group>
          </el-select>
        </el-form-item>
        <el-form-item label="数据名称" prop="webTitle">
          <el-input v-model="form.webTitle" placeholder="请输入数据名称" />
        </el-form-item>
        <el-form-item label="栏目名称" prop="columnTitle">
          <el-input v-model="form.columnTitle" placeholder="请输入栏目名称" />
        </el-form-item>
        <el-form-item label="是否本地化图片">
          <el-radio-group v-model="form.picDownload">
            <el-radio
              v-for="dict in dict.type.sys_yes_no"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="研究领域" prop="resarchArea">
          <el-select v-model="form.resarchArea" placeholder="请选择研究领域">
            <el-option
              v-for="dict in dict.type.academic_area"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="初始URL" prop="spiderUrl">
          <el-input v-model="form.spiderUrl" placeholder="请输入初始化URL" />
        </el-form-item>
        <el-form-item label="页码正则" prop="pageXpath">
          <el-input v-model="form.pageXpath" placeholder="请输入页码正则" />
        </el-form-item>
        <el-form-item label="页码区域正则" prop="pageDivXpath">
          <el-input v-model="form.pageDivXpath" placeholder="请输入页码区域正则" />
        </el-form-item>
        <el-form-item label="默认的字符编码" prop="charset">
          <el-input v-model="form.charset" placeholder="请输入默认的字符编码" />
        </el-form-item>
        <el-form-item label="列表正则" prop="listXpath">
          <el-input v-model="form.listXpath" placeholder="请输入列表正则" />
        </el-form-item>
        <el-form-item label="详情正则" prop="detailsXpath">
          <el-input v-model="form.detailsXpath" placeholder="请输入详情正则" />
        </el-form-item>
        <el-form-item label="avator" prop="avatorXpath">
          <el-input v-model="form.avatorXpath" placeholder="请输入avator" />
        </el-form-item>
        <el-form-item label="标题正则" prop="titleXpath">
          <el-input v-model="form.titleXpath" placeholder="请输入标题正则" />
        </el-form-item>
        <el-form-item label="发布机构正则" prop="publisherXpath">
          <el-input v-model="form.publisherXpath" placeholder="请输入发布机构正则" />
        </el-form-item>
        <el-form-item label="摘要正则" prop="summaryXpath">
          <el-input v-model="form.summaryXpath" placeholder="请输入摘要正则" />
        </el-form-item>
        <el-form-item label="发布时间正则" prop="publishTimeXpath">
          <el-input v-model="form.publishTimeXpath" placeholder="请输入发布时间正则" />
        </el-form-item>
        <el-form-item label="全文正则" prop="contentsXpath">
          <el-input v-model="form.contentsXpath" placeholder="请输入全文正则" />
        </el-form-item>
        <el-form-item label="标签正则" prop="labelXpath">
          <el-input v-model="form.labelXpath" placeholder="请输入标签正则" />
        </el-form-item>
        <el-form-item label="附件正则" prop="filesXpath">
          <el-input v-model="form.filesXpath" placeholder="请输入附件正则" />
        </el-form-item>
        <el-form-item label="延时(秒)" prop="intervalNum">
          <el-input v-model="form.intervalNum" placeholder="请输入爬虫延时" />
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in dict.type.sys_job_status"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 选择标签弹框 -->
    <el-dialog :title="labelSet.title" :visible.sync="labelSet.open" width="400px" append-to-body>
      <el-table ref="labelTable" :data="this.labelSet.labelList" @selection-change="handleLabelSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="分类名称" align="center" prop="labelName" />
        <el-table-column label="分类代码" align="center" prop="labelCode" />
        <el-table-column label="备注" align="center" prop="remark" />
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="updateLabel">确 定</el-button>
        <el-button @click="labelSet.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSources, getSources, delSources, addSources, updateSources, exportSources, updateSourcesLabel } from "@/api/information/sources";
import { listLabelFirst} from "@/api/information/labelFirst";

export default {
  name: "Sources",
  dicts: ['academic_area', 'sys_job_status', 'data_type', 'sys_yes_no', 'source_type', 'disaster_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 数据源管理表格数据
      sourcesList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        country: null,
        webTitle: null,
        resarchArea: null,
        dataType: null,
        status: null,
        sourceType: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        webTitle: [
          { required: true, message: "数据源名称不能为空", trigger: "blur" }
        ],
        columnTitle: [
          { required: true, message: "栏目名称不能为空", trigger: "blur" }
        ],
        spiderUrl: [
          { required: true, message: "初始化URL不能为空", trigger: "blur" }
        ],
      },
      country:[{
        label: '常搜国家',
        options: [{value:'China',label:'中国'},]
      },{
        label: '所有国家',
        options: [
          {value:'Angola',label:'安哥拉'},
          {value:'Afghanistan',label:'阿富汗'},
          {value:'Albania',label:'阿尔巴尼亚'},
          {value:'Algeria',label:'阿尔及利亚'},
          {value:'Andorra',label:'安道尔共和国'},
          {value:'Anguilla',label:'安圭拉岛'},
          {value:'Antigua and Barbuda',label:'安提瓜和巴布达'},
          {value:'Argentina',label:'阿根廷'},
          {value:'Armenia',label:'亚美尼亚'},
          {value:'Ascension',label:'阿森松'},
          {value:'Australia',label:'澳大利亚'},
          {value:'Austria',label:'奥地利'},
          {value:'Azerbaijan',label:'阿塞拜疆'},
          {value:'Bahamas',label:'巴哈马'},
          {value:'Bahrain',label:'巴林'},
          {value:'Bangladesh',label:'孟加拉国'},
          {value:'Barbados',label:'巴巴多斯'},
          {value:'Belarus',label:'白俄罗斯'},
          {value:'Belgium',label:'比利时'},
          {value:'Belize',label:'伯利兹'},
          {value:'Benin',label:'贝宁'},
          {value:'Bermuda Is',label:'百慕大群岛'},
          {value:'Bolivia',label:'玻利维亚'},
          {value:'Botswana',label:'博茨瓦纳'},
          {value:'Brazil',label:'巴西'},
          {value:'Brunei',label:'文莱'},
          {value:'Bulgaria',label:'保加利亚'},
          {value:'Burkina Faso',label:'布基纳法索'},
          {value:'Burma',label:'缅甸'},
          {value:'Burundi',label:'布隆迪'},
          {value:'Cameroon',label:'喀麦隆'},
          {value:'Canada',label:'加拿大'},
          {value:'Cayman Is',label:'开曼群岛'},
          {value:'Central African Republic',label:'中非共和国'},
          {value:'Chad',label:'乍得'},
          {value:'Chile',label:'智利'},
          {value:'China',label:'中国'},
          {value:'Colombia',label:'哥伦比亚'},
          {value:'Congo',label:'刚果'},
          {value:'Cook Is',label:'库克群岛'},
          {value:'Costa Rica',label:'哥斯达黎加'},
          {value:'Cuba',label:'古巴'},
          {value:'Cyprus',label:'塞浦路斯'},
          {value:'Czech Republic',label:'捷克'},
          {value:'Denmark',label:'丹麦'},
          {value:'Djibouti',label:'吉布提'},
          {value:'Dominica Rep',label:'多米尼加共和国'},
          {value:'Ecuador',label:'厄瓜多尔'},
          {value:'Egypt',label:'埃及'},
          {value:'EI Salvador',label:'萨尔瓦多'},
          {value:'Estonia',label:'爱沙尼亚'},
          {value:'Ethiopia',label:'埃塞俄比亚'},
          {value:'Fiji',label:'斐济'},
          {value:'Finland',label:'芬兰'},
          {value:'France',label:'法国'},
          {value:'French Guiana',label:'法属圭亚那'},
          {value:'French Polynesia',label:'法属玻利尼西亚'},
          {value:'Gabon',label:'加蓬'},
          {value:'Gambia',label:'冈比亚'},
          {value:'Georgia',label:'格鲁吉亚'},
          {value:'Germany',label:'德国'},
          {value:'Ghana',label:'加纳'},
          {value:'Gibraltar',label:'直布罗陀'},
          {value:'Greece',label:'希腊'},
          {value:'Grenada',label:'格林纳达'},
          {value:'Guam',label:'关岛'},
          {value:'Guatemala',label:'危地马拉'},
          {value:'Guinea',label:'几内亚'},
          {value:'Guyana',label:'圭亚那'},
          {value:'Haiti',label:'海地'},
          {value:'Honduras',label:'洪都拉斯'},
          {value:'Hungary',label:'匈牙利'},
          {value:'Iceland',label:'冰岛'},
          {value:'India',label:'印度'},
          {value:'Indonesia',label:'印度尼西亚'},
          {value:'Iran',label:'伊朗'},
          {value:'Iraq',label:'伊拉克'},
          {value:'Ireland',label:'爱尔兰'},
          {value:'Israel',label:'以色列'},
          {value:'Italy',label:'意大利'},
          {value:'Ivory Coast',label:'科特迪瓦'},
          {value:'Jamaica',label:'牙买加'},
          {value:'Japan',label:'日本'},
          {value:'Jordan',label:'约旦'},
          {value:'Kampuchea (Cambodia )',label:'柬埔寨'},
          {value:'Kazakstan',label:'哈萨克斯坦'},
          {value:'Kenya',label:'肯尼亚'},
          {value:'Korea',label:'韩国'},
          {value:'Kuwait',label:'科威特'},
          {value:'Kyrgyzstan',label:'吉尔吉斯坦'},
          {value:'Laos',label:'老挝'},
          {value:'Latvia',label:'拉脱维亚'},
          {value:'Lebanon',label:'黎巴嫩'},
          {value:'Lesotho',label:'莱索托'},
          {value:'Liberia',label:'利比里亚'},
          {value:'Libya',label:'利比亚'},
          {value:'Liechtenstein',label:'列支敦士登'},
          {value:'Lithuania',label:'立陶宛'},
          {value:'Luxembourg',label:'卢森堡'},
          {value:'Madagascar',label:'马达加斯加'},
          {value:'Malawi',label:'马拉维'},
          {value:'Malaysia',label:'马来西亚'},
          {value:'Maldives',label:'马尔代夫'},
          {value:'Mali',label:'马里'},
          {value:'Malta',label:'马耳他'},
          {value:'Mariana Is',label:'马里亚那群岛'},
          {value:'Martinique',label:'马提尼克'},
          {value:'Mauritius',label:'毛里求斯'},
          {value:'Mexico',label:'墨西哥'},
          {value:'Moldova',label:'摩尔多瓦'},
          {value:'Monaco',label:'摩纳哥'},
          {value:'Mongolia',label:'蒙古'},
          {value:'Montserrat Is',label:'蒙特塞拉特岛'},
          {value:'Morocco',label:'摩洛哥'},
          {value:'Mozambique',label:'莫桑比克'},
          {value:'Namibia',label:'纳米比亚'},
          {value:'Nauru',label:'瑙鲁'},
          {value:'Nepal',label:'尼泊尔'},
          {value:'Netheriands Antilles',label:'荷属安的列斯'},
          {value:'Netherlands',label:'荷兰'},
          {value:'New Zealand',label:'新西兰'},
          {value:'Nicaragua',label:'尼加拉瓜'},
          {value:'Niger',label:'尼日尔'},
          {value:'Nigeria',label:'尼日利亚'},
          {value:'North Korea',label:'朝鲜'},
          {value:'Norway',label:'挪威'},
          {value:'Oman',label:'阿曼'},
          {value:'Pakistan',label:'巴基斯坦'},
          {value:'Panama',label:'巴拿马'},
          {value:'Papua New Cuinea',label:'巴布亚新几内亚'},
          {value:'Paraguay',label:'巴拉圭'},
          {value:'Peru',label:'秘鲁'},
          {value:'Philippines',label:'菲律宾'},
          {value:'Poland',label:'波兰'},
          {value:'Portugal',label:'葡萄牙'},
          {value:'Puerto Rico',label:'波多黎各'},
          {value:'Qatar',label:'卡塔尔'},
          {value:'Reunion',label:'留尼旺'},
          {value:'Romania',label:'罗马尼亚'},
          {value:'Russia',label:'俄罗斯'},
          {value:'Saint Lueia',label:'圣卢西亚'},
          {value:'Saint Vincent',label:'圣文森特岛'},
          {value:'Samoa Eastern',label:'东萨摩亚(美)'},
          {value:'Samoa Western',label:'西萨摩亚'},
          {value:'San Marino',label:'圣马力诺'},
          {value:'Sao Tome and Principe',label:'圣多美和普林西比'},
          {value:'Saudi Arabia',label:'沙特阿拉伯'},
          {value:'Senegal',label:'塞内加尔'},
          {value:'Seychelles',label:'塞舌尔'},
          {value:'Sierra Leone',label:'塞拉利昂'},
          {value:'Singapore',label:'新加坡'},
          {value:'Slovakia',label:'斯洛伐克'},
          {value:'Slovenia',label:'斯洛文尼亚'},
          {value:'Solomon Is',label:'所罗门群岛'},
          {value:'Somali',label:'索马里'},
          {value:'South Africa',label:'南非'},
          {value:'Spain',label:'西班牙'},
          {value:'SriLanka',label:'斯里兰卡'},
          {value:'St.Lucia',label:'圣卢西亚'},
          {value:'St.Vincent',label:'圣文森特'},
          {value:'Sudan',label:'苏丹'},
          {value:'Suriname',label:'苏里南'},
          {value:'Swaziland',label:'斯威士兰'},
          {value:'Sweden',label:'瑞典'},
          {value:'Switzerland',label:'瑞士'},
          {value:'Syria',label:'叙利亚'},
          {value:'Tajikstan',label:'塔吉克斯坦'},
          {value:'Tanzania',label:'坦桑尼亚'},
          {value:'Thailand',label:'泰国'},
          {value:'Togo',label:'多哥'},
          {value:'Tonga',label:'汤加'},
          {value:'Trinidad and Tobago',label:'特立尼达和多巴哥'},
          {value:'Tunisia',label:'突尼斯'},
          {value:'Turkey',label:'土耳其'},
          {value:'Turkmenistan',label:'土库曼斯坦'},
          {value:'Uganda',label:'乌干达'},
          {value:'Ukraine',label:'乌克兰'},
          {value:'United Arab Emirates',label:'阿拉伯联合酋长国'},
          {value:'United Kiongdom',label:'英国'},
          {value:'United States of America',label:'美国'},
          {value:'Uruguay',label:'乌拉圭'},
          {value:'Uzbekistan',label:'乌兹别克斯坦'},
          {value:'Venezuela',label:'委内瑞拉'},
          {value:'Vietnam',label:'越南'},
          {value:'Yemen',label:'也门'},
          {value:'Yugoslavia',label:'南斯拉夫'},
          {value:'Zimbabwe',label:'津巴布韦'},
          {value:'Zaire',label:'扎伊尔'},
          {value:'Zambia',label:'赞比亚'}
        ]
      }],
      labelSet: {
        open: false,
        title: "",
        labelList: [],
        total: 0,
        loading: true,
        sourceId: "",
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          country: null,
          webTitle: null,
          resarchArea: null,
          status: null,
          sourceType: null,
        },
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
      },

    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询数据源管理列表 */
    getList() {
      this.loading = true;
      listSources(this.queryParams).then(response => {
        this.sourcesList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        sourceId: null,
        country: null,
        webTitle: null,
        columnTitle: null,
        resarchArea: null,
        spiderUrl: null,
        pageXpath: null,
        pageDivXpath: null,
        charset: null,
        listXpath: null,
        detailsXpath: null,
        avatorXpath: null,
        titleXpath: null,
        publisherXpath: null,
        summaryXpath: null,
        publishTimeXpath: null,
        contentsXpath: null,
        labelXpath: null,
        filesXpath: null,
        status: "0",
        dataType: null,
        lastUpdate: null,
        picDownload: "N",
        intervalNum: 3,
        tenantId: null,
        sourceType: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.sourceId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加数据源";
    },
    handleLabel(row) {
      this.labelSet.title = "选择标签";
      this.labelSet.open = true;
      this.labelSet.sourceId = row.sourceId;
      listLabelFirst(this.labelSet.queryParams).then(response => {
        this.labelSet.labelList = response.rows;
        this.labelSet.total = response.total;
        this.labelSet.loading = false;
      }).then(()=>{
          for(let i=0;i<this.labelSet.labelList.length;i++){
            console.log(row.labelIds.split(",").indexOf(this.labelSet.labelList[i].id.toString()))
            if(row.labelIds != '' && row.labelIds.split(",").indexOf(this.labelSet.labelList[i].id.toString()) >= 0){
              this.$refs.labelTable.toggleRowSelection(this.labelSet.labelList[i], true);
            }
          }
        })
    },
    // 多选框选中数据
    handleLabelSelectionChange(selection) {
      console.log(selection);
      this.labelSet.ids = selection.map(item => item.id)
      this.labelSet.single = selection.length!==1
      this.labelSet.multiple = !selection.length
    },
    updateLabel(){
      console.log(this.labelSet.ids);
      updateSourcesLabel(this.labelSet.sourceId, this.labelSet.ids).then(response => {
        this.$modal.msgSuccess("修改成功");
        this.labelSet.open = false;
        this.getList();
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const sourceId = row.sourceId || this.ids
      getSources(sourceId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改数据源";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.sourceId != null) {
            updateSources(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSources(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const sourceIds = row.sourceId || this.ids;
      this.$modal.confirm('是否确认删除数据源编号为"' + sourceIds + '"的数据项？').then(function() {
        return delSources(sourceIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$modal.confirm('是否确认导出所有数据源数据项？').then(() => {
        this.exportLoading = true;
        return exportSources(queryParams);
      }).then(response => {
        this.$download.name(response.msg);
        this.exportLoading = false;
      }).catch(() => {});
    }
  }
};
</script>
